package com.csun.cmny.provider.controller;

import cn.jiguang.common.utils.Preconditions;
import com.alibaba.fastjson.JSON;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.ActivityQueryDto;
import com.csun.cmny.provider.dto.OldmanQueryDto;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.WelfareActivityDto;
import com.csun.cmny.provider.model.dto.WelfareIntroduceDto;
import com.csun.cmny.provider.model.dto.WelfareMessageDto;
import com.csun.cmny.provider.model.vo.QueryMachineOldInfoVo;
import com.csun.cmny.provider.model.vo.ReportFormCountVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcQueryMachineService;
import com.csun.cmny.provider.validator.WelfareActivityAddValidator;
import com.csun.cmny.provider.validator.WelfareActivityEditValidator;
import com.csun.cmny.provider.validator.WelfareIntroduceSaveValidator;
import com.csun.cmny.provider.validator.WelfareMessageSaveValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/queryMachine", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcQueryMachineController extends BaseController {

    @Resource
    private OmcQueryMachineService omcQueryMachineService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 报表统计
     * @param: []
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/reportFormCount")
    public Wrapper reportFormCount() {

        ReportFormCountVo reportFormCountVo  = omcQueryMachineService.reportFormCount(getLoginAuthDto());

        //return WrapMapper.ok(reportFormCountVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),reportFormCountVo);
    }

    /**
     * 机构介绍-保存
     * @param: [welfareIntroduceDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/saveIntroduce")
    public Wrapper saveIntroduce(@Validated(WelfareIntroduceSaveValidator.class) @RequestBody WelfareIntroduceDto welfareIntroduceDto, BindingResult br) {

        int ret = omcQueryMachineService.saveIntroduce(welfareIntroduceDto,getLoginAuthDto());

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 机构介绍-查询
     * @param: [welfareIntroduceDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getIntroduceByIntroduceType/{type}")
    public Wrapper getIntroduceByIntroduceType(@PathVariable Integer type){

        // 校验Id是否为空
        Preconditions.checkArgument(type != null, cmnyMessageSource
                .getMessage("queryMachine.introduceType.empty", null));

        List<OmcWelfareIntroduce> wiList = omcQueryMachineService.getIntroduceByIntroduceType(type,getLoginAuthDto());
        //return WrapMapper.ok(wiList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),wiList);
    }


    /**
     * 机构信息-保存
     * @param: [welfareMessageDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/saveMessage")
    public Wrapper saveMessage(@Validated(WelfareMessageSaveValidator.class) @RequestBody WelfareMessageDto welfareMessageDto, BindingResult br) {

        int ret = omcQueryMachineService.saveMessage(welfareMessageDto,getLoginAuthDto());
        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 机构信息-查询
     * @param: []
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getMessage")
    public Wrapper getMessage() {

        List<OmcWelfareMessage> wmList = omcQueryMachineService.getMessage(getLoginAuthDto());

        //return WrapMapper.ok(wmList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),wmList);
    }

    /**
     * 福利院活动-添加
     * @param: [welfareActivityDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/addActivity")
    public Wrapper addActivity(@Validated(WelfareActivityAddValidator.class) @RequestBody WelfareActivityDto welfareActivityDto, BindingResult br) {

        int ret = saveWelfareActivity(welfareActivityDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 福利院活动-编辑
     * @param: [welfareActivityDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/editActivity")
    public Wrapper editActivity(@Validated(WelfareActivityEditValidator.class) @RequestBody WelfareActivityDto welfareActivityDto, BindingResult br) {

        int ret = saveWelfareActivity(welfareActivityDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 根据活动id删除活动
     * @param: [activityId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteByActivityId/{activityId}")
    public Wrapper deleteByActivityId(@PathVariable Long activityId) {

        // 校验Id是否为空
        Preconditions.checkArgument(activityId != null, cmnyMessageSource
                .getMessage("queryMachine.activityId.null", null));
        int ret = omcQueryMachineService.deleteByActivityId(activityId);

        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 分页查询活动
     * @param: [activityId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryActivitiesWithPage")
    public Wrapper queryActivitiesWithPage(@RequestBody ActivityQueryDto activityQueryDto) {

        PageInfo pageInfo = omcQueryMachineService.queryActivitiesWithPage(activityQueryDto,getLoginAuthDto());

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /**
     * 轮播图-添加
     * @param: [picId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/addActivityCarousel/{picString}")
    public Wrapper addActivityCarousel(@PathVariable String picString) {

        // 校验Id是否为空
        Preconditions.checkArgument(picString != null, cmnyMessageSource
                .getMessage("queryMachine.picId.null", null));
        int ret = omcQueryMachineService.saveActivityCarousel(picString,getLoginAuthDto());
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 轮播图-删除
     * @param: [picId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteCarouselById/{carouselId}")
    public Wrapper deleteCarouselById(@PathVariable Long carouselId) {

        // 校验Id是否为空
        Preconditions.checkArgument(carouselId != null, cmnyMessageSource
                .getMessage("queryMachine.picId.null", null));

        int ret = omcQueryMachineService.deleteCarouselById(carouselId,getLoginAuthDto());

        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 轮播图-查询
     * @param: [picId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryCarouselList")
    public Wrapper queryCarouselList() {

        OmcWelfareActivityCarousel wac = omcQueryMachineService.queryCarouselList(getLoginAuthDto());

        //return WrapMapper.ok(wac);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),wac);
    }

    /**
     * 今日天气
     * @param: []
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getTodayWeather")
    public Wrapper getTodayWeather() {

        String json = omcQueryMachineService.getTodayWeather();
        Map map = (Map) JSON.parseObject(json);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), map);
    }

    /**
     * 老人信息查询
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getInfoByOldmanId/{oldmanId}")
    public Wrapper getOldmanInfoByOldmanId(@PathVariable Long oldmanId){
        // 校验Id是否为空

        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        QueryMachineOldInfoVo vo = omcQueryMachineService.getOldmanInfoByOldmanId(oldmanId,getLoginAuthDto());
        //return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),vo);
    }

    /**
     * 老人周期项目查询
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getOldmanCycleItem/{oldmanId}")
    public Wrapper getOldmanCycleItem(@PathVariable Long oldmanId){
        // 校验Id是否为空

        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));
        List<OmcCycleExpenseItem> itemList = omcQueryMachineService.getOldmanCycleItem(oldmanId,getLoginAuthDto());
        //return WrapMapper.ok(itemList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),itemList);
    }

    /**
     * 老人列表查询
     * @param: [oldmanId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryOldmanList")
    public Wrapper queryOldmanList(@RequestBody OldmanQueryDto oldmanQueryDto){

        PageInfo pageInfo = omcQueryMachineService.queryOldmanList(oldmanQueryDto,getLoginAuthDto());
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    //福利院活动-保存
    public Integer saveWelfareActivity(WelfareActivityDto welfareActivityDto) {
        OmcWelfareActivity welfareActivity = new OmcWelfareActivity();
        BeanUtils.copyProperties(welfareActivityDto, welfareActivity);
        return omcQueryMachineService.saveWelfareActivity(welfareActivity, getLoginAuthDto());
    }

    /**
     * 机构介绍-查询
     * @param: [welfareIntroduceDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getIntroduceByGroupId/{groupId}/{type}")
    public Wrapper getIntroduceByGroupId(@PathVariable Integer type,@PathVariable Long groupId){

        // 校验Id是否为空
        Preconditions.checkArgument(type != null, cmnyMessageSource
                .getMessage("queryMachine.introduceType.empty", null));

        List<OmcWelfareIntroduce> wiList = omcQueryMachineService.getIntroduceByGroupId(type,groupId);
        //return WrapMapper.ok(wiList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),wiList);
    }

}
